from sintax import sintax
#
# 	ANALISADOR LEXICO
#
class scanner:
	def __init__(self, nameoffile):
		# instacia objetos de sintaxes
		self.sintax = sintax()
		self.fim = False
		self.list_line = []
		self.posinline = 0
		self.bufftokens=[]
		self.file = open(nameoffile, "r")
	
	def separa_delimitador(self, obj):
		if not (obj == None):
		#elif self.index
			print ""
			print "DELIMITADOR:" + str(obj)
			self.e_delimitador = False
			self.list = {} 	# delimitadores encontrados
			items_deli = []  # indice da posicao deles
			index_ant = 0
			# Buscando na string se e encontrado
			for deli in self.sintax.delimitador:
				strsear = obj.find(deli)
				if strsear <> -1:
					print deli +"-> " + str(obj.split(deli))
					self.list[int(index_ant)] = (str(obj[index_ant:strsear]), index_ant)
					self.list[int(strsear)] = (deli, strsear)
					items_deli.append(strsear)
			print ""
			print self.list
			items_deli.sort()
			for i in items_deli:
				print str(i) + "->" + str(self.list[i])
				
	def bloco(self):
		pass
	def gettipo(self, obj):
		pass
	
	def tokenisclear(self, strtoken):
		ret = []
		for indd in self.sintax.delimitador:
			if strtoken.find(indd)<> -1:
				ret.append(indd)
		for indd in self.sintax.operador:
			if strtoken.find(indd)<> -1:
				ret.append(indd)
		

	def addtokensdelimiter(self, strtoken, delimiter):
		print "fragmente %s per %s" % (strtoken, delimiter)
		for i in strtoken.split(delimiter):
			print "TOKEN F: ", str(i)
			
	def getnexttoken(self):
		print "LEN: %i"% len(self.bufftokens) 
		if len(self.bufftokens)>1:
			print self.tokens
			ret = self.tokens.pop()
			print ret
			return ret 
		else:
			ln = self.get_line()
			print ln
			ls = ln.split(" ")
			#print ls
			for i in ls:
				print i
				for indd in self.sintax.delimitador:
					self.addtokensdelimiter(i, indd)
				for indd in self.sintax.operador:
					self.addtokensdelimiter(i, indd)
			
	def getnexttoken_old(self):
		obj = self.getnextstr()
		self.separa_delimitador(obj)
		return (obj, self.gettipo(obj))
	
	#
	#  PEGA A PROXIMA STRING DENTRO DO ARQUIVO
	# 

	def getnextstr(self):
		if len(self.list_line)>0:
			return self.list_line.pop()
		else:
			lnf = self.file.readline()
			self.list_line = lnf.replace("\n", "").replace("\r","").split(" ")
			self.list_line.reverse()
			if lnf == "":
					self.fim = True
					return None
			return self.getnextstr()
			
			
	def start(self, token):
		print "start"
	
	#
	# Vai no arquivo ate buscar um delimitador final . ou ;
	# 
	def get_line(self):
		return "a=bc+c;"
		
		
if __name__=="__main__":
	p = scanner("teste.pas")
	#p.start()
	#p.compile_line("if a=b then b=c;")
	#while not p.fim:
	obj =p.getnexttoken()
	#	if not (obj == (None,None)):
	#		print obj
	#	else:
	#		print "fim"
